package org.xq.shop.maxaishop.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import oracle.jrockit.jfr.VMJFR;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.util.UriComponentsBuilder;
import org.xq.shop.maxaishop.entity.XqCustomer;
import org.xq.shop.maxaishop.service.XqCustomerService;
import org.xq.shop.maxaishop.util.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (XqCustomer)表控制层
 *
 * @author makejava
 * @since 2021-06-10 18:01:37
 */
@Slf4j
@Api( tags = "用户管理与操作")
@RestController
@RequestMapping("xqCustomer")
public class XqCustomerController {
    /**
     * 服务对象
     */
    @Resource
    private XqCustomerService xqCustomerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据主键查询单个系统用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required=true,paramType="query" )
    @GetMapping("selectOne")
    public XqCustomer selectOne(Integer id) {
        return this.xqCustomerService.queryById(id);
    }

    /**
     * 用户登录
     * @param name
     * @param pwd
     * @return
     */
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="用户名",required=true,paramType="query"),
            @ApiImplicitParam(name="pwd",value="密码",required=true,paramType="query"),
    })
    @PostMapping("login")
    public Map queryByNameAndPwd (String name , String pwd) {
        String s = DigestUtils.md5DigestAsHex(pwd.getBytes());
        XqCustomer shopCustomer = this.xqCustomerService.queryByNameAndPwd(name, s);
        if ( shopCustomer == null) {
            return JsonRes.failure(null);
        }else if(shopCustomer.getCustomerStauts()==0){
            return JsonRes.failure(null);
        }else {
            return JsonRes.success(shopCustomer);
        }


    }

    @ApiOperation("用户重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="用户名",required=true,paramType="query"),
            @ApiImplicitParam(name="tel",value="电话号码",required=true,paramType="query"),
    })
    @PostMapping("reset")
    public Map reset(String uname,String tel){
        XqCustomer xqCustomer = this.xqCustomerService.selectByUnameAndTel(uname, tel);
        System.out.println(xqCustomer);
        if (xqCustomer!=null){
            int reset = this.xqCustomerService.reset(uname, tel);
            return JsonRes.success(reset);
        }else {
            return JsonRes.failure(null);
        }

    }

    /**
     * 用户注册
     * @return
     */
    @ApiOperation("这是用户注册接口")
    @PostMapping("regist")
    public int registered(XqCustomer xqCustomer){
        xqCustomer.setCustomerPwd(DigestUtils.md5DigestAsHex(xqCustomer.getCustomerPwd().getBytes()));
        xqCustomer.setCustomerImg("https://rjb-xqshoping.oss-cn-beijing.aliyuncs.com/shop/putong/20181118142953_yaktg.jpeg");
        Jedis jedis = new Jedis();
        String code = jedis.get("CODE"); // 验证码
        if (xqCustomer.getYzm().equals(code)){
            XqCustomer insert = this.xqCustomerService.insert(xqCustomer);
            if (insert!=null){
                return 1;
            }else {

                return 0;
            }
        }else {
            return -1;
        }

    }


    /**
     * 查询所有
     * @param page
     * @param limit
     * @param uname
     * @return
     */
    @ApiOperation("用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页面",required=true,paramType="query"),
            @ApiImplicitParam(name="limit",value="每页行数",required=true,paramType="query"),
            @ApiImplicitParam(name="uname",value="用户名",paramType="query",dataType="String")
    })
 @GetMapping( "selectAll")
    public  Result<XqCustomer>  selectAll( Integer page, Integer limit,String uname){
        int start = (page-1)*limit;
        Result<XqCustomer> result = new Result<XqCustomer>();
        System.out.println(uname);
        if (uname==null){
            List<XqCustomer> xqCustomers = this.xqCustomerService.selectAll(start, limit);
            int count = this.xqCustomerService.selectCount();
            System.out.println(count);
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCustomers);

        }else if (uname=="") {
            List<XqCustomer> xqCustomers = this.xqCustomerService.selectAll(start, limit);
            int count = this.xqCustomerService.selectCount();
            System.out.println(count);
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCustomers);
        }else {
                List<XqCustomer> xqCustomers = this.xqCustomerService.selectByUname(uname, start, limit);
                int count = this.xqCustomerService.countt(uname);
                result.setCode(200);
                result.setCount(count);
                result.setMsg("请求成功");
                result.setData(xqCustomers);
            }

     return  result;
    }

    /**
     * 通过id删除
     * @param customerId
     * @return
     */
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "customerId", value = "用户ID", required=true,paramType="query" )
    @GetMapping("deleteById")
    public boolean deleteById(Integer customerId){
        boolean b = this.xqCustomerService.deleteById(customerId);
        return b;
    }

    /**
     * 用户修改
     * @param xqCustomer
     * @return
     */
    @ApiOperation("修改用户信息")
    @PostMapping("update")
    public Map update( @RequestBody XqCustomer xqCustomer){
        int update = this.xqCustomerService.update(xqCustomer);
        if (update==1){
            return JsonRes.success(update);
        }else {
            return JsonRes.failure(null);
        }
    }

    /**
     * 选中删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除用户")
    @PostMapping ("XzDel")
    public int XzDel(String ids){
        String[] split = ids.split(",");
        for (String str:split) {
            this.xqCustomerService.deleteById(Integer.parseInt(str));
        }
        return 200;
    }

    /**
     * 修改密码
     * @param xqCustomer
     * @return
     */
    @ApiOperation("修改密码")
    @PostMapping("updatePwd")
    public int updatePwd(XqCustomer xqCustomer){
        XqCustomer xqCustomer1 = this.xqCustomerService.queryById(xqCustomer.getCustomerId());
//        System.out.println(DigestUtils.md5DigestAsHex(xqCustomer.getYpwd().getBytes()));
        if (xqCustomer1.getCustomerPwd().equals(DigestUtils.md5DigestAsHex(xqCustomer.getYpwd().getBytes()))){
            xqCustomer.setCustomerPwd(DigestUtils.md5DigestAsHex(xqCustomer.getCustomerPwd().getBytes()));
       this.xqCustomerService.updatePwd(xqCustomer);
            return 200;
        }else {
            return 100;
        }

    }
    @ApiOperation("通过用户名查询用户信息")
    @ApiImplicitParam(name = "uname", value = "用户名", required=true,paramType="query" )
    @PostMapping("selectByname")
    public boolean selectByname(String uname){
        XqCustomer xqCustomer = this.xqCustomerService.selectByname(uname);
        return xqCustomer != null;
    }


    @ApiOperation("用户注册邮箱验证")
    @ApiImplicitParam(name = "email", value = "用户邮箱", required=true,paramType="query" )
    @PostMapping("email")
    private String email(String email){
        YzmUtil yzmUtil = new YzmUtil();
        String r = yzmUtil.getCheckCode();
        // 将验证码保存到redis数据库内
        Jedis jedis = new Jedis();
        jedis.del("CODE");
        jedis.set("CODE", r);
        // 给验证码设置过期时间 存活五分钟或验证码自动过期
        jedis.expire("CODE", 60 * 5);
        String result ="欢迎注册惊鸿果蔬商城系统，您的验证码是"+r+"，有效期为5分钟，请尽快前往注册";
        MailUtils.sendMail(email,result ,r);
        return result;
    }






}
